package com.example.proyecto;

/**
 * Rafael Mendez
 * Rafael Berganza
 * Rodrigo Avelar
 * Carlos Alvarez
 * Jose Angel Estrada
 */

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" })
public class Ejecutar_programa extends Activity {
	
	private final String NAME = "scripts"; 
	private String texto,posicion;
	private TextView text;
	ImageView aniView;
	float posx, posy;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ejecutar_programa);
        aniView = (ImageView) findViewById(R.id.imageView5);
        posx = aniView.getX();
        posy = aniView.getY();    
        texto="";
        posicion=posx +" "+ posy;
        text = (TextView) findViewById(R.id.textView1);
        text.setText(posicion);
        Intent intent = getIntent();
        texto=intent.getStringExtra(NAME);
        
    }
    
    
    public void Reset(View view) {
    	posx = 43;
    	posy = 43;
    	ObjectAnimator animation1 = ObjectAnimator.ofFloat(aniView,"y", posy);
	  	animation1.setDuration(0);
	  	animation1.start();
	  	ObjectAnimator animation2 = ObjectAnimator.ofFloat(aniView,"x", posx);
	  	animation2.setDuration(0);
	  	animation2.start();
    }
    
    public void StartMove(View view) {
    	float dest = 0;
    	String instruccion[] = texto.split(","); 
    	ObjectAnimator animationx; 
    	posx = aniView.getX();
        posy = aniView.getY();
    	for(int i=0;i<instruccion.length;i++){
    		String ins = instruccion[i];
    		
    		if(ins.equals("adelante")){
    			posy = posy + 10;
    			if(posy>200){
    		  		dest = 360;
    		  		posy = 200;
    		  	      if (aniView.getRotation() == 360) {
    		  	        System.out.println(aniView.getAlpha());
    		  	        dest = 0;
    		  	      }
    		  	      animationx = ObjectAnimator.ofFloat(aniView,"rotation", dest);
    		  	      animationx.setDuration(2000);
    		  	      animationx.start();
    		  	}
    			posicion=posx +" "+ posy;  
    		  	text.setText(posicion);
    		  	ObjectAnimator animation4 = ObjectAnimator.ofFloat(aniView,"y", posy);
    		  	animation4.setDuration(1500);
    		  	animation4.start();
    		}  	
    		if(ins.equals("atras")){
    			posy = posy - 10;
    	    	if(posy<43){
    	    		dest = 360;
    	    		posy = 43;
    	    	      if (aniView.getRotation() == 360) {
    	    	        System.out.println(aniView.getAlpha());
    	    	        dest = 0;
    	    	      }
    	    	      animationx = ObjectAnimator.ofFloat(aniView,"rotation", dest);
    	    	      animationx.setDuration(2000);
    	    	      animationx.start();
    	    	}
    	    	posicion=posx +" "+ posy;  
    		  	text.setText(posicion);
    	    	ObjectAnimator animation1 = ObjectAnimator.ofFloat(aniView,"y", posy);
    	    	animation1.setDuration(1500);
    	    	animation1.start();	
    	    	
    		}
    		if(ins.equals("izquierda")){
    			posx = posx-10 ;
    	    	if(posx<43){
    	    		dest = 360;
    	    		posx = 43;
    	    	      if (aniView.getRotation() == 360) {
    	    	        System.out.println(aniView.getAlpha());
    	    	        dest = 0;
    	    	      }
    	    	      animationx = ObjectAnimator.ofFloat(aniView,"rotation", dest);
    	    	      animationx.setDuration(2000);
    	    	      animationx.start();
    	    	}    	
    	        posicion = posx +" "+ posy;        
    	        text.setText(posicion);
    	        ObjectAnimator animation3 = ObjectAnimator.ofFloat(aniView,"x", posx);
    	        animation3.setDuration(1500);
    	        animation3.start();
    			
    		}
    		if(ins.equals("derecha")){
    			posx = posx+10 ;
    		      if(posx>233){
    		  		dest = 360;
    		  		posx = 233;
    		  	      if (aniView.getRotation() == 360) {
    		  	        System.out.println(aniView.getAlpha());
    		  	        dest = 0;
    		  	      }
    		  	      animationx = ObjectAnimator.ofFloat(aniView,"rotation", dest);
    		  	      animationx.setDuration(2000);
    		  	      animationx.start();
    		  	}
    		      posicion = posx +" "+ posy;        
      	          text.setText(posicion);
    		      ObjectAnimator animation2 = ObjectAnimator.ofFloat(aniView,
    		          "x", posx);
    		      animation2.setDuration(1500);
    		      animation2.start();
    		}
    	}
    
    }


}
